<?php 

/**
 * 货品列表控制器
 */
class ListController extends AuthController{
	private $model;
	
	
	public function __auto(){
		$this->model = K('List');
	}
	
	/**
	 * 显示页面
	 */
	
	public function index(){
		$gid = Q('get.gid',0,'intval');
//		p($gid);
		
		if(IS_POST){
//			p($_POST);
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加货品成功');
		}
		$addData = M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.idtype_attr')->where("goods_gid={$gid} AND class=1")->group('name')->order('gtid ASC')->all();
//		p($addData);
		foreach ($addData as $k => $v) {
//					p($v['taid']);
			$addData[$k]['value'] = K('Goods_Attr')->where("goods_gid={$gid} AND taid={$v['taid']}")->all();
		}
//		p($addData);
		$this->assign('addData',$addData);
		
		//显示当前商品的货品列表数据
		$showData = K('List')->where("goods_gid={$gid}")->all();
//		p($showData);
		foreach ($showData as $k => $v) {
			$showData[$k]['value'] = K('Goods_Attr')->where("gtid in({$v['combine']})")->all();
		}
//		p($showData);
		$this->assign('showData',$showData);
		$this->display();
	}
	
	/**
	 * 删除
	 */
	public function del(){
		$glid = Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		$this->success("删除成功");
	}	
	
	/**
	 * 编辑
	 */
	public function edit(){
		//获取gid
		$gid = Q('get.gid',0,'intval');
		//获取glid
		$glid = Q('get.glid',0,'intval');
		
		if(IS_POST){
			if(!$this->model->editData($glid)){
				$this->error($this->model->error);
			}
			$this->success('修改成功',U('index',array('gid'=>$gid)));
		}
		//获得旧数据
		$oldData = $this->model->where("glid={$glid}")->find();
//		p($oldData);
		$oldData['combine']=explode(',', $oldData['combine']);
//		p($oldData);die;
		$this->assign('oldData',$oldData);
		
		
		//顶部标题和底下下拉框数据
		$addData = M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.idtype_attr')->where("goods_gid={$gid} AND class=1")->group('name')->order('gtid ASC')->all();
//		p($addData);
		foreach ($addData as $k => $v) {
//					p($v['taid']);
			$addData[$k]['value'] = K('Goods_Attr')->where("goods_gid={$gid} AND taid={$v['taid']}")->all();
		}
//		p($addData);
		$this->assign('addData',$addData);
		$this->display();
	}
	
	
}










 ?>